﻿/// <reference path="../GeneratedArtifacts/viewModel.js" />

myapp.FindPlace.created = function (screen) {
    //Default values
    if (screen.MaxDistance == null) {
        screen.MaxDistance = 1000;
    }
    if (screen.MaxCount == null) {
        screen.MaxCount = 10;
    }
    if (screen.PlaceIsShared == null) {
        screen.PlaceIsShared = true;
    }
    if (screen.useBingMap == null) {
        screen.useBingMap = true;
    }

    //events
};

myapp.FindPlace.Step1_postRender = function (element, contentItem) {
    contentItem.screen.details.displayName = "Favorites";
};

myapp.FindPlace.ShowStep1_Back_execute = function (screen) {
    myapp.cancelChanges();
};
myapp.FindPlace.ShowStep1_Next_execute = function (screen) {
    if (screen.PlaceId > 0) {
        myapp.commitChanges();
    }
    else {
        screen.showTab("Step2");
        screen.details.displayName = "Options";
    }
};

myapp.FindPlace.ShowStep2_Back_execute = function (screen) {
    screen.showTab("Step1");
    screen.details.displayName = "Favorites";
};
myapp.FindPlace.ShowStep2_Next_execute = function (screen) {
    if (screen.PlaceId > 0){
        myapp.commitChanges();
        return;
    }
    screen.showTab("Step3");
    screen.details.displayName = "Map";
};

myapp.FindPlace.ShowStep3_Back_execute = function (screen) {
    screen.showTab("Step2");
    screen.details.displayName = "Options";
};
myapp.FindPlace.ShowStep3_Next_execute = function (screen) {
    screen.showTab("Step4");
    screen.details.displayName = "Places";
};

myapp.FindPlace.ShowStep4_Back_execute = function (screen) {
    screen.showTab("Step3");
    screen.details.displayName = "Map";
};


myapp.FindPlace.PlacesFiltered_ItemTap_execute = function (screen) {
    screen.PlaceId = screen.PlacesFiltered.selectedItem.Id;
    //myapp.commitChanges();
};

myapp.FindPlace.FavoritesFiltered_ItemTap_execute = function (screen) {
    screen.PlaceId = screen.FavoritesFiltered.selectedItem.Place.Id;
    //myapp.commitChanges();
};

myapp.FindPlace.MaxDistance_render = function (element, contentItem) {
    $(element).append('<input type="range" min="0" max="10000" step="100" value="' + contentItem.value + '" />');
    // Now, after jQueryMobile has had a chance to process the new DOM addition, perform our own post-processing:
    setTimeout(function () {
        var $slider = $('input', $(element));
        // If the content item changes (perhaps due to another control being
        //     bound to the same content item, or if a change occurs programmatically), 
        //     update the visual representation of the slider:
        contentItem.dataBind('value', function (newValue) {
            $slider.val(newValue);
        });

        // Conversely, whenever the user adjusts the slider visually, update the underlying content item:
        $slider.change(function () {
            contentItem.value = $slider.val();
            contentItem.screen.MaxDistnace = contentItem.value;
        });
    }, 0);
};


myapp.FindPlace.FindNearestPlaces_execute = function (screen) {
    if (foundPlace != null) {
        GetNearestPlaces(screen, foundPlace);
    }
    else {
        if (navigator && navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                foundPlace = { Address: "Current Position", Latitude: position.coords.latitude, Longitude: position.coords.longitude };
                GetNearestPlaces(screen, foundPlace);
            }, geo_error);
        }
        else {
            alert("Geolocation is not supported/allowed by this browser.");
        }
    }
};

function GetNearestPlaces(screen, centerPlace) {
    if (screen.useBingMap) {
        $(bingMapDiv).lightswitchBingMapsControl("setCenter", centerPlace);
    }
    else {
        $(googleMapDiv).lightswitchGoogleMapsControl("setCenter", centerPlace);
    }
    $.ajax({
        type: 'get',
        data: {
            Longitude: centerPlace.Longitude, //138,
            Latitude: centerPlace.Latitude, // - 35,
            MaxDistance: screen.MaxDistnace,
            MaxCount: screen.MaxCount,
            MaxPrice: screen.MaxPrice,
            MaxHours: screen.MaxHours,
            MaxMinutes: screen.MaxMinutes
            //&DateTime? Date = null, bool? Occupied = null
        },
        url: '../api/Places',
        success: function (response) {
            //apply filtering...
            /*if (screen.useBingMap) {
                $(bingMapDiv).lightswitchBingMapsControl("getStaticPositions", response, foundPlace, screen.PlacesFiltered);
            }
            else {
                $(googleMapDiv).lightswitchGoogleMapsControl("getStaticPositions", response, foundPlace);
            }*/
        }
    });
};

var bingMapDiv;
myapp.FindPlace.bingMap_render = function (element, contentItem) {
    bingMapDiv = $('<div id="bmap_canvas" style="width: 100%; height: 100%" />').appendTo($(element));
    $(bingMapDiv).lightswitchBingMapsControl();
    $(bingMapDiv).lightswitchBingMapsControl("resetMap");
    setTimeout(function () {
        if (navigator && navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var fromPlace = { Address: "Current Position", Latitude: position.coords.latitude, Longitude: position.coords.longitude };
                $(bingMapDiv).lightswitchBingMapsControl("getCurrentPosition", fromPlace);
            }, geo_error);
        }
        else {
            alert("Geolocation is not supported/allowed by this browser.");
        }
    }, 1000);
};

var googleMapDiv;
myapp.FindPlace.googleMap_render = function (element, contentItem) {
    googleMapDiv = $('<div id="gmap_canvas" style="width: 100%; height: 100%" />').appendTo($(element)); //class="gmap3" 
    $(googleMapDiv).lightswitchGoogleMapsControl();
    setTimeout(function () {
        if (navigator && navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var fromPlace = { Address: "Current Position", Latitude: position.coords.latitude, Longitude: position.coords.longitude };
                $(googleMapDiv).lightswitchGoogleMapsControl("getCurrentPosition", fromPlace);
            }, geo_error);
        }
        else {
            alert("Geolocation is not supported/allowed by this browser.");
        }
    }, 0);
};

myapp.FindPlace.useBingMap_render = function (element, contentItem) {
    createMapSwitch(element, contentItem, 'Bing', 'Google', '10em');
};

myapp.FindPlace.DrivingMode_render = function (element, contentItem) {
    createBooleanSwitch(element, contentItem, 'Walking', 'Driving', '', '10em');
};

myapp.FindPlace.directionsItinerary_render = function (element, contentItem) {
    /*var directionsItineraryDiv = $('#directionsItinerary');
    if (directionsItineraryDiv != null) {
        $(this).remove();
    }*/
    var directionsItineraryDiv = $('<div id="directionsItinerary"></div>').appendTo($(element)); //style="border : width : 200px; height : 50px; "
};

myapp.FindPlace.TrafficMode_render = function (element, contentItem) {
    createBooleanSwitch(element, contentItem, 'Show', 'Hide', '', '10em');
};

myapp.FindPlace.ShowNearestPlaces_execute = function (screen) {
    if (screen.useBingMap) {
        $(bingMapDiv).lightswitchBingMapsControl("getCenter", function (center) {
            $.ajax({
                type: 'get',
                data: {
                    Longitude: center.longitude,
                    Latitude: center.latitude,
                    MaxDistance: screen.MaxDistnace,
                    MaxCount: screen.MaxCount,
                    MaxPrice: screen.MaxPrice,
                    MaxHours: screen.MaxHours,
                    MaxMinutes: screen.MaxMinutes
                    //&DateTime? Date = null, bool? Occupied = null
                },
                url: '../api/Places',
                success: function (response) {
                    $(bingMapDiv).lightswitchBingMapsControl("getStaticPositions", response, function (placeId) {
                        screen.PlaceId = placeId;
                    });
                }
            });
        });
    }
    else {
        $(googleMapDiv).lightswitchGoogleMapsControl("getCenter", function (center) {
            $.ajax({
                type: 'get',
                data: {
                    Longitude: center.lng(),
                    Latitude: center.lat(),
                    MaxDistance: screen.MaxDistnace,
                    MaxCount: screen.MaxCount,
                    MaxPrice: screen.MaxPrice,
                    MaxHours: screen.MaxHours,
                    MaxMinutes: screen.MaxMinutes
                    //&DateTime? Date = null, bool? Occupied = null
                },
                url: '../api/Places',
                success: function (response) {
                    $(googleMapDiv).lightswitchGoogleMapsControl("getStaticPositions", response, function (placeId) {
                        screen.PlaceId = placeId;
                    });
                }
            });
        });
    }

};

myapp.FindPlace.ShowStep4_Finish_execute = function (screen) {
    if (screen.PlaceId > 0) {
        myapp.commitChanges();
    }
    else {
        myapp.cancelChanges();
    }
};